package com.zxk.controller;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.app.dto.TaskMappingDto;
import com.zxk.app.vo.AppStartVo;
import com.zxk.service.MyTask;
import com.zxk.service.TaskProgressService;
import com.zxk.service.impl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;


/**
 * 任务控制器，主要对业务层进行组合
 *
 * @author: 舟小亢
 * @date: 2023-08-05 22:26
 */

public class TaskController {

    private static Logger LOG = LoggerFactory.getLogger(TaskController.class);

    public TaskController(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) throws Exception {
        // 按任务顺序初始化实例
        initTask(hwnd_0, hwnd_1, hwnd_2);

        // 开始执行任务
        startTask();
    }

    private LinkedList<MyTask> taskList = new LinkedList<>();

    private void initTask(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) throws Exception {
        for (String value : AppStartVo.TASK_MAP.values()) {
            switch (value) {
                /**
                 * 登录任务
                 */
                case TaskMappingDto.LOGIN_GAME:
                    taskList.add(new LoginGameImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                /**
                 * 日常单人任务
                 */
                case TaskMappingDto.SCRATCH_TICKET:
                    taskList.add(new ScratchTicketTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.USE_ENERGY:
                    taskList.add(new UseEnergyTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.AQ_TASK:
                    taskList.add(new AQTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.EXAM_TASK:
                    taskList.add(new ExamTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.MAP_TASK:
                    taskList.add(new MapTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.SECRET_AREA:
                    taskList.add(new SecretAreaTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.DART_CAR:
                    taskList.add(new DartCarTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.USE_MAP:
                    taskList.add(new UseMapTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.SELL_SILVER_COIN:
                    taskList.add(new SellSilverCoinImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.RECEIVE_POINT:
                    taskList.add(new ReceivePointTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                /**
                 * 日常多人任务
                 */
                case TaskMappingDto.HARD_INSTANCE_1:
                    taskList.add(new HardInstance1TaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.NORMAL_INSTANCE_1:
                    taskList.add(new NormalInstance1TaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.NORMAL_INSTANCE_2:
                    taskList.add(new NormalInstance2TaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.FIGHT_GHOST:
                    taskList.add(new FightGhostTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                /**
                 * 活动任务
                 */
                case TaskMappingDto.CAT_EDEN:
                    taskList.add(new CatEdenTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.CLIMB_TOWER:
                    taskList.add(new ClimbTowerTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.GANG_TASK:
                    taskList.add(new GangTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                case TaskMappingDto.RODEO_DOWN_SCORE:
                    taskList.add(new RodeDownScoreTaskImpl(hwnd_0, hwnd_1, hwnd_2));
                    break;
                default:
                    break;
            }
        }
    }

    private void startTask() throws Exception {
        for (MyTask myTask : taskList) {
            // 记录当前任务信息
            TaskProgressService.updateProgress(myTask.getTaskName(),"0%");
            if (!myTask.goHome()) {
                // 初始化失败则放弃本次任务
                continue;
            }
            TaskProgressService.updateProgress(myTask.getTaskName(),"30%");
            if (!myTask.execute()) {
                // 执行失败则放弃本次任务
                continue;
            }
            TaskProgressService.updateProgress(myTask.getTaskName(),"50%");
            if (!myTask.checkTask()) {
                // 执行失败则放弃本次任务
                continue;
            }
            TaskProgressService.updateProgress(myTask.getTaskName(),"100%");
        }
    }
}
